#include "CDirectWrite.h"
#include "CRender.h"


CDirectWrite::CDirectWrite()
{
	
}


CDirectWrite::~CDirectWrite()
{

}


CDirectWrite* CDirectWrite::GetInstance()
{
	static CDirectWrite write;
	return &write;
}


bool CDirectWrite::InitFonts()
{
	ID3DX10Font* defaultFont;
	HRESULT hr = D3DX10CreateFont(CRender::GetInstance()->GetD3DDevice(), 35, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Helvetica", &defaultFont);

	if(FAILED(hr))
	{
		return false;
	}
	m_vFonts.push_back(defaultFont);

	hr =  D3DX10CreateFont(CRender::GetInstance()->GetD3DDevice(), 16, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Helvetica", &defaultFont);
	if(FAILED(hr))
	{
		return false;
	}
	m_vFonts.push_back(defaultFont);

	hr =  D3DX10CreateFont(CRender::GetInstance()->GetD3DDevice(), 35, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "EARTH", &defaultFont);
	if(FAILED(hr))
	{
		return false;
	}
	m_vFonts.push_back(defaultFont);


	D3DX10CreateSprite(CRender::GetInstance()->GetD3DDevice(), 1024, &m_pFontSprite);

	D3D10_BLEND_DESC StateDesc;
	ZeroMemory(&StateDesc, sizeof(D3D10_BLEND_DESC));
	StateDesc.AlphaToCoverageEnable = FALSE;
	StateDesc.BlendEnable[0] = TRUE;
	StateDesc.SrcBlend = D3D10_BLEND_SRC_ALPHA;
	StateDesc.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
	StateDesc.BlendOp = D3D10_BLEND_OP_ADD;
	StateDesc.SrcBlendAlpha = D3D10_BLEND_ZERO;
	StateDesc.DestBlendAlpha = D3D10_BLEND_ZERO;
	StateDesc.BlendOpAlpha = D3D10_BLEND_OP_ADD;
	StateDesc.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;
	CRender::GetInstance()->GetD3DDevice()->CreateBlendState(&StateDesc, &m_pFontBlendState10);

	return true;
}


void CDirectWrite::DrawText(m_eFonts eType, std::string text, int xOffset, int yOffset, D3DXCOLOR color)
{
	FLOAT OriginalBlendFactor[4];
	UINT  OriginalSampleMask = 0;

	CRender::GetInstance()->GetD3DDevice()->OMGetBlendState(&m_pOriginalBlendState10, OriginalBlendFactor, &OriginalSampleMask);

	RECT rect;
	SetRectEmpty(&rect);
	GetFontRectangle(eType, text, &rect);

	rect.left += xOffset;
	rect.right += xOffset;
	rect.top += yOffset;
	rect.bottom += yOffset;

	m_pFontSprite->Begin(D3DX10_SPRITE_SORT_TEXTURE);

	m_vFonts[eType]->DrawTextA(m_pFontSprite, text.c_str(), -1, &rect, DT_LEFT, color);

	if(m_pFontBlendState10)
	{
		FLOAT NewBlendFactor[4] = {0,0,0,0};
		CRender::GetInstance()->GetD3DDevice()->OMSetBlendState(m_pFontBlendState10, NewBlendFactor, 0xffffffff);
	}

	m_pFontSprite->End();

	CRender::GetInstance()->GetD3DDevice()->OMSetBlendState(m_pOriginalBlendState10, OriginalBlendFactor, OriginalSampleMask);

}


void CDirectWrite::ShutDown()
{
	// Release the original blend state object
	if (m_pOriginalBlendState10 != NULL)
	{
		m_pOriginalBlendState10->Release();
		m_pOriginalBlendState10 = NULL;
	}

	// Release the font blend state object
	if (m_pFontBlendState10 != NULL)
	{
		m_pFontBlendState10->Release();
		m_pFontBlendState10 = NULL;
	}

	// Release the font
	while(!m_vFonts.empty())
	{
		if(m_vFonts.back() != NULL)
		{
			m_vFonts.back()->Release();
			m_vFonts.pop_back();
		}
	}

	// Release the font sprite
	if (m_pFontSprite != NULL)
	{
		m_pFontSprite->Release();
		m_pFontSprite = NULL;
	}
}


void CDirectWrite::GetFontRectangle(m_eFonts eType, std::string text, RECT* rect)
{
	m_vFonts[eType]->DrawTextA(NULL, text.c_str(), -1, rect, DT_CALCRECT | DT_LEFT, D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f));
}